/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is Forte for Java, Community Edition. The Initial * Developer of the Original Code is Sun Microsystems, Inc. Portions * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved. */ package org.openide.explorer.propertysheet.editors; import java.awt.Component; import java.util.ResourceBundle; import java.util.StringTokenizer; import javax.swing.*; import org.openide.*; import org.openide.src.*; import org.openide.util.NbBundle; import org.openide.util.Utilities; /** * Panel for editing the arrays of any kind of Object. * This is an abstract class which should be subclassed * for the specified types of arrays (e.g. Identifier[], * MethodParameter[], ...) * * @author Petr Hamernik */ abstract class ObjectArrayPanel extends javax.swing.JPanel { /** Resource bundle. */ static final ResourceBundle bundle = NbBundle.getBundle(ObjectArrayPanel.class); /** The list model for the edited data. */ protected DefaultListModel model; /** Creates new form ListPanel */ public ObjectArrayPanel() { initComponents (); model = new DefaultListModel(); list.setModel(model); updateButtons(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the FormEditor. */ private void initComponents () {//GEN-BEGIN:initComponents jScrollPane1 = new javax.swing.JScrollPane (); list = new javax.swing.JList (); upButton = new javax.swing.JButton (); downButton = new javax.swing.JButton (); jPanel1 = new javax.swing.JPanel (); addButton = new javax.swing.JButton (); removeButton = new javax.swing.JButton (); changeButton = new javax.swing.JButton (); setLayout (new java.awt.GridBagLayout ()); java.awt.GridBagConstraints gridBagConstraints1; setBorder (new javax.swing.border.EmptyBorder(new java.awt.Insets(5, 5, 5, 5))); list.setVisibleRowCount (3); list.setSelectionMode (javax.swing.ListSelectionModel.SINGLE_SELECTION); list.addListSelectionListener (new javax.swing.event.ListSelectionListener () { public void valueChanged (javax.swing.event.ListSelectionEvent evt) { listValueChanged (evt); } } ); list.addMouseListener (new java.awt.event.MouseAdapter () { public void mouseClicked (java.awt.event.MouseEvent evt) { listMouseClicked (evt); } } ); jScrollPane1.setViewportView (list); gridBagConstraints1 = new java.awt.GridBagConstraints (); gridBagConstraints1.gridheight = 2; gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints1.weightx = 1.0; gridBagConstraints1.weighty = 1.0; add (jScrollPane1, gridBagConstraints1); upButton.setText (bundle.getString("CTL_MoveUp")); upButton.addActionListener (new java.awt.event.ActionListener () { public void actionPerformed (java.awt.event.ActionEvent evt) { upButtonActionPerformed (evt); } } ); gridBagConstraints1 = new java.awt.GridBagConstraints (); gridBagConstraints1.gridwidth = 0; gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints1.insets = new java.awt.Insets (6, 6, 6, 6); add (upButton, gridBagConstraints1); downButton.setText (bundle.getString("CTL_MoveDown")); downButton.addActionListener (new java.awt.event.ActionListener () { public void actionPerformed (java.awt.event.ActionEvent evt) { downButtonActionPerformed (evt); } } ); gridBagConstraints1 = new java.awt.GridBagConstraints (); gridBagConstraints1.gridwidth = 0; gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints1.insets = new java.awt.Insets (0, 6, 6, 6); gridBagConstraints1.anchor = java.awt.GridBagConstraints.NORTH; add (downButton, gridBagConstraints1); jPanel1.setLayout (new java.awt.GridLayout (1, 3, 8, 0)); addButton.setText (bundle.getString("CTL_Add")); addButton.addActionListener (new java.awt.event.ActionListener () { public void actionPerformed (java.awt.event.ActionEvent evt) { addButtonActionPerformed (evt); } } ); jPanel1.add (addButton); removeButton.setText (bundle.getString("CTL_Remove")); removeButton.addActionListener (new java.awt.event.ActionListener () { public void actionPerformed (java.awt.event.ActionEvent evt) { removeButtonActionPerformed (evt); } } ); jPanel1.add (removeButton); changeButton.setText (bundle.getString("CTL_Change")); changeButton.addActionListener (new java.awt.event.ActionListener () { public void actionPerformed (java.awt.event.ActionEvent evt) { changeButtonActionPerformed (evt); } } ); jPanel1.add (changeButton); gridBagConstraints1 = new java.awt.GridBagConstraints (); gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints1.insets = new java.awt.Insets (6, 6, 6, 6); add (jPanel1, gridBagConstraints1); }//GEN-END:initComponents private void listMouseClicked (java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listMouseClicked if (org.openide.awt.MouseUtils.isDoubleClick(evt)) { int selIndex = list.getSelectedIndex(); if (selIndex != -1) { Object newValue = editValue(model.getElementAt(selIndex)); if (newValue != null) { model.setElementAt(newValue,selIndex); modelChanged(); updateButtons(); } } } }//GEN-LAST:event_listMouseClicked private void listValueChanged (javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listValueChanged updateButtons(); }//GEN-LAST:event_listValueChanged private void downButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downButtonActionPerformed int selIndex = list.getSelectedIndex(); model.set(selIndex + 1, model.set(selIndex, model.getElementAt(selIndex + 1))); list.setSelectedIndex(selIndex + 1); modelChanged(); updateButtons(); }//GEN-LAST:event_downButtonActionPerformed private void upButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_upButtonActionPerformed int selIndex = list.getSelectedIndex(); model.set(selIndex - 1, model.set(selIndex, model.getElementAt(selIndex - 1))); list.setSelectedIndex(selIndex - 1); modelChanged(); updateButtons(); }//GEN-LAST:event_upButtonActionPerformed private void changeButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeButtonActionPerformed int selIndex = list.getSelectedIndex(); Object newValue = editValue(model.getElementAt(selIndex)); if (newValue != null) { model.setElementAt(newValue,selIndex); modelChanged(); updateButtons(); } }//GEN-LAST:event_changeButtonActionPerformed private void removeButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed int selIndex = list.getSelectedIndex(); model.remove(selIndex); int size = model.size(); if (size > 0) { selIndex = Math.min(size - 1, selIndex); list.setSelectedIndex(selIndex); } modelChanged(); updateButtons(); }//GEN-LAST:event_removeButtonActionPerformed private void addButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed Object newValue = insertNewValue(); if (newValue != null) { model.addElement(newValue); list.setSelectedIndex(model.size() - 1); modelChanged(); updateButtons(); } }//GEN-LAST:event_addButtonActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables javax.swing.JScrollPane jScrollPane1; javax.swing.JList list; javax.swing.JButton upButton; javax.swing.JButton downButton; javax.swing.JPanel jPanel1; javax.swing.JButton addButton; javax.swing.JButton removeButton; javax.swing.JButton changeButton; // End of variables declaration//GEN-END:variables /** Method for subclasses. * @return the main JList component in the panel */ protected JList getListComponent() { return list; } /** Updates the 'enable' status of the buttons in the panel. */ private void updateButtons() { boolean isEmpty = model.isEmpty(); int selIndex = list.getSelectedIndex(); boolean isSelected = (selIndex != -1); removeButton.setEnabled(isSelected); changeButton.setEnabled(isSelected); upButton.setEnabled(isSelected && (selIndex > 0)); downButton.setEnabled(isSelected && (selIndex < model.size() - 1)); } /** Ask user for new value. * @return new value or <CODE>null</CODE> when * operation was canceled. */ protected abstract Object insertNewValue(); /** Ask user for edit value. * @param oldValue The previous value to be edited * @return new value or <CODE>null</CODE> when * operation was canceled. */ protected abstract Object editValue(Object oldValue); /** Notification about the changes in the data array. */ protected void modelChanged() { } }